from ..base_downloader import *
from ..base_downloader_firefox import BaseDownloaderFirefox


class Luyan(BaseDownloaderFirefox):
    def __init__(self):
        super().__init__()
        self.head = "鹭燕医药"

    def login(self):
        username_field = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.NAME, "username"))
        )
        password_field = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.NAME, "loginpwd"))
        )
        username_field.send_keys(self.username)
        password_field.send_keys(self.password)
        dropdown_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CLASS_NAME, "t_input3"))
        )

        dropdown = Select(dropdown_element)
        dropdown.select_by_value(self.head)

        self.capture_screenshot()

        self.driver.find_element(By.XPATH, "//input[@class='buttonsubmit' and @value='登录查询']").click()

        time.sleep(0.5)
        self.accept_alert_dialog()
        time.sleep(1.5)

    def retrieve_data(self):
        self.navigate_to_relative_url("/ws/index.php/Cyberseek/index")
        self.set_start_and_end_dates()
        self.click_retrieve_button()

    def page_html_to_excel(self):
        self.driver.switch_to.default_content()
        data_frame = pd.read_html(self.driver.page_source)[0]
        target_path = os.path.join(
            self.task_download_dir, f"{self.site_name}_page_html_to_excel.xlsx"
        )
        data_frame.to_excel(target_path, index=False)

    def export_excel(self):
        self.capture_screenshot()
        export_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, "//input[@class='stdButton' and @value='导出Excel']"))
        )
        export_button.click()
        downloaded_file = self.get_latest_downloaded_file()

        new_file_name = f"{self.site_name}_exported_click_button.xlsx"
        destination_path = os.path.join(self.task_download_dir, new_file_name)
        if is_xml_file(downloaded_file):
            convert_xml_to_xlsx(downloaded_file, destination_path)

        logger.info(f"Copied and renamed file to: {destination_path}")

    def click_retrieve_button(self):
        try:
            retrieve_button = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "//input[@class='stdButton' and @value='开始查询']"))
            )
            retrieve_button.click()
        except Exception as e:
            logger.exception(f"An error occurred while clicking the retrieve button: {e}")

    def set_start_and_end_dates(
        self, start_date="2024-05-01", end_date=date.today().strftime("%Y-%m-%d")
    ):
        start_date_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, "//input[@class='Wdate' and @name='StartDate']")
            )
        )
        start_date_input.clear()  
        start_date_input.send_keys(start_date)

        end_date_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(
                (By.XPATH, "//input[@class='Wdate' and @name='EndDate']")
            )
        )
        end_date_input.clear()
        end_date_input.send_keys(end_date)

        self.capture_screenshot()

    def download_file(self):
        self.page_html_to_excel()  # One way to download the Excel file
        self.export_excel()        # Another way to download the Excel file; use either method


class LuyanYiyao(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "鹭燕医药"


class LuyanFuzhou(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "福州鹭燕"


class LuyanZhangzhou(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "漳州鹭燕"


class LuyanQuanzhou(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "泉州鹭燕"


class LuyanLongyan(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "龙岩新鹭燕"


class LuyanNingde(Luyan):
    def __init__(self):
        super().__init__()
        self.head = "宁德鹭燕"
